package com.example.timeplanning_teacher;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.mob.MobSDK;


import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * Created by siner on 2017/6/30.
 */

public class CaptchaActivity extends Activity implements OnClickListener{

    private EditText edit_phone;
    private EditText edit_cord;
    private Button btn_getCord;
    private Button btn_register;
    private String phone_number;
    private String cord_number;
    private String name;
    private String username;
    private SharedPreferences sp;
    private boolean flag=true;

    EventHandler eventHandler = new EventHandler() {
        public void afterEvent(int event, int result, Object data) {
            // afterEvent会在子线程被调用，因此如果后续有UI相关操作，需要将数据发送到UI线程
            Message msg = new Message();
            msg.arg1 = event;
            msg.arg2 = result;
            msg.obj = data;
            new Handler(Looper.getMainLooper(), new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    int event = msg.arg1;
                    int result = msg.arg2;
                    Object data = msg.obj;
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            // TODO 处理成功得到验证码的结果
                            Toast.makeText(getApplicationContext(), "验证码发送成功", Toast.LENGTH_LONG).show();
                        } else {
                            // TODO 处理错误的结果
                            Toast.makeText(getApplicationContext(), "验证码发送失败", Toast.LENGTH_LONG).show();
                            ((Throwable) data).printStackTrace();
                        }
                    } else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        if (result == SMSSDK.RESULT_COMPLETE) {
                            // TODO 处理验证码验证通过的结果
                            Toast.makeText(getApplicationContext(), "验证码正确", Toast.LENGTH_LONG).show();
                            toWhichActivity(name);
                        } else {
                            // TODO 处理错误的结果
                            Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_LONG).show();
                            ((Throwable) data).printStackTrace();
                        }
                    }
                    // TODO 其他接口的返回结果也类似，根据event判断当前数据属于哪个接口
                    return false;
                }
            }).sendMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_captcha);
        sp = getSharedPreferences("parent", Context.MODE_PRIVATE);
        username = sp.getString("username","");
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("Message");
        name = bundle.getString("name");


        getId();

        edit_phone.setInputType(InputType.TYPE_CLASS_PHONE);

        SMSSDK.registerEventHandler(eventHandler);
        if(name == "toChangePsd") edit_phone.setText(username);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }
    private void toWhichActivity(String name){
        Intent intent = null;
        switch (name){
            case "toRegister":
                Bundle bundle = new Bundle();
                intent =new Intent(CaptchaActivity.this, RegisterActivity.class);
                bundle.putString("phnum",phone_number);
                intent.putExtra("Message",bundle);
                break;
            case "toChangePsd":

                intent =new Intent(CaptchaActivity.this, ChangePsdActivity.class);
                break;
            default:
                break;
        }
        startActivity(intent);
        CaptchaActivity.this.finish();
    }

    /**
     * 获取id
     */
    private void getId()
    {
        edit_phone=findViewById(R.id.edit_phone);
        edit_cord=findViewById(R.id.edit_code);
        btn_getCord=findViewById(R.id.btn_getcord);
        btn_register=findViewById(R.id.btn_register);
        btn_getCord.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    /**
     * 按钮点击事件
     */
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.btn_getcord:
                if(judPhone())//去掉左右空格获取字符串
                {
                    SMSSDK.getVerificationCode("86",phone_number);
                    edit_cord.requestFocus();
                }
                break;
            case R.id.btn_register:
                if(judCord())
                    SMSSDK.submitVerificationCode("86",phone_number,cord_number);
                //flag=false;
                break;
            default:
                break;
        }
    }

    private boolean judPhone()
    {
        if(TextUtils.isEmpty(edit_phone.getText().toString().trim()))
        {
            Toast.makeText(CaptchaActivity.this,"请输入您的电话号码",Toast.LENGTH_LONG).show();
            edit_phone.requestFocus();
            return false;
        }
        else if(edit_phone.getText().toString().trim().length()!=11)
        {
            Toast.makeText(CaptchaActivity.this,"您的电话号码位数不正确",Toast.LENGTH_LONG).show();
            edit_phone.requestFocus();
            return false;
        }
        else
        {
            phone_number=edit_phone.getText().toString().trim();
            String num="[1][345789]\\d{9}";
            if(phone_number.matches(num))
                return true;
            else
            {
                Toast.makeText(CaptchaActivity.this,"请输入正确的手机号码",Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }

    private boolean judCord()
    {
        judPhone();
        if(TextUtils.isEmpty(edit_cord.getText().toString().trim()))
        {
            Toast.makeText(CaptchaActivity.this,"请输入您的验证码",Toast.LENGTH_LONG).show();
            edit_cord.requestFocus();
            return false;
        }
        else if(edit_cord.getText().toString().trim().length()!=6)
        {
            Toast.makeText(CaptchaActivity.this,"您的验证码位数不正确",Toast.LENGTH_LONG).show();
            edit_cord.requestFocus();

            return false;
        }
        else
        {
            cord_number=edit_cord.getText().toString().trim();
            return true;
        }

    }



}

